package com.sun.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @Author Sun
 * @Date 2020/4/24 16:13
 * @Version 1.0
 * @Description 操作数据库的公共类
 */


public class BaseDao {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;

    //静态代码块，类加载的时候就初始化
    static {
        //通过类加载器读取资源流
        InputStream resourceAsStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

        Properties properties = new Properties();

        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver=properties.getProperty("driver");
        url=properties.getProperty("url");
        user=properties.getProperty("user");
        password=properties.getProperty("password");

    }

    //获取数据库连接
    public static Connection getConnection() throws Exception {

        Connection connection = null;
        Class.forName(driver);
        connection = DriverManager.getConnection(url,user,password);

        return connection;
    }

    //编写查询公共类
    public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] params) throws SQLException {
        //预编译SQL
        preparedStatement = connection.prepareStatement(sql);

        //放置参数
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1, params[i]);
        }

        //执行查询
        resultSet = preparedStatement.executeQuery();

        //返回结果集
        return  resultSet;
        
    }

    //编写增删改公共类
    public static int execute(Connection connection, PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException {
        //预编译SQL
        preparedStatement = connection.prepareStatement(sql);

        //放置参数
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1, params[i]);
        }

        //执行增删改
        int affectRows = preparedStatement.executeUpdate();

        //返回结果集
        return  affectRows;

    }

    public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){

        boolean flag = true;

        if(connection!=null){
            try {
                connection.close();
                //GC回收
                connection=null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if(preparedStatement!=null){
            try {
                preparedStatement.close();
                //GC回收
                preparedStatement=null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if(resultSet!=null){
            try {
                resultSet.close();
                //GC回收
                resultSet=null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;//如果有一个关闭失败就返回false

    }

}
